package com.atguigu.tingshu.order.pattern.strategy;

import cn.hutool.core.lang.Assert;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.order.pattern.AbstractTradeOrderStrategy;
import com.atguigu.tingshu.user.client.UserFeignClient;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import static com.atguigu.tingshu.common.constant.SystemConstant.ORDER_DERATE_VIP_SERVICE_DISCOUNT;

/**
 * 商品类别为VIP：策略实现类
 * @author: atguigu
 * @create: 2025-03-25 16:24
 */
@Component("1003")
public class VIPTradeOrderStrategy extends AbstractTradeOrderStrategy {

    @Autowired
    private UserFeignClient userFeignClient;

    /**
     * 处理结算商品类别为：VIP会员
     * @param tradeVo
     * @return
     */
    @Override
    public void handleTrade(TradeVo tradeVo, OrderInfoVo orderInfoVo) {
        //2.1 远程调用"用户服务"获取套餐详情
        VipServiceConfig vipServiceConfig = userFeignClient.getVipServiceConfig(tradeVo.getItemId()).getData();
        Assert.notNull(vipServiceConfig, "套餐:{}不存在", tradeVo.getItemId());
        //2.2 计算订单相关价格 订单价=原价-减免价
        BigDecimal originalAmount = vipServiceConfig.getPrice();
        BigDecimal orderAmount = vipServiceConfig.getDiscountPrice();
        BigDecimal derateAmount = originalAmount.subtract(orderAmount);

        //2.3 封装订单明细列表
        List<OrderDetailVo> orderDetailVoList = new ArrayList<>();
        OrderDetailVo orderDetailVo = new OrderDetailVo();
        orderDetailVo.setItemId(tradeVo.getItemId());
        orderDetailVo.setItemName("VIP套餐：" + vipServiceConfig.getName());
        orderDetailVo.setItemUrl(vipServiceConfig.getImageUrl());
        orderDetailVo.setItemPrice(originalAmount);
        orderDetailVoList.add(orderDetailVo);

        //2.4 封装订单优惠列表
        List<OrderDerateVo> orderDerateVoList = new ArrayList<>();
        if (originalAmount.compareTo(orderAmount) == 1) {
            OrderDerateVo orderDerateVo = new OrderDerateVo();
            //订单减免类型 1405-专辑折扣 1406-VIP服务折
            orderDerateVo.setDerateType(ORDER_DERATE_VIP_SERVICE_DISCOUNT);
            orderDerateVo.setDerateAmount(derateAmount);
            orderDerateVo.setRemarks("套餐限时减免：" + derateAmount);
            orderDerateVoList.add(orderDerateVo);
        }
        orderInfoVo.setOriginalAmount(originalAmount);
        orderInfoVo.setOrderAmount(orderAmount);
        orderInfoVo.setDerateAmount(derateAmount);
        //5.2 为商品列表、优惠列表赋值
        orderInfoVo.setOrderDetailVoList(orderDetailVoList);
        orderInfoVo.setOrderDerateVoList(orderDerateVoList);

        //5.3 为订单VO其他杂项赋值：购买商品类别
        orderInfoVo.setItemType(tradeVo.getItemType());
        //5.4 流水号、时间戳及签名
        this.generateTradeNo(orderInfoVo);
        this.generateSign(orderInfoVo);
    }
}
